import { defineStore, createPinia } from 'pinia'
import { MusicList, User, isValidKey, SocialInfo } from '@/type'
export const pinia = createPinia()
export const useStore = defineStore('GlobalInfo', {
    state: () => ({
        baseUrl: 'http://www.mmdccj.top:9003',
        UserInfo: {
            account: ''
        } as User,
        socialInfo: {
            "concerns": 0,
            "fans": 0,
            "post": 0
        },
        myMusicList: [] as MusicList[],
        /***
         * 显示一些用户数据
         * 0 暂时不加载 
         * 1 登录
         * -1 未登录
         */
        isLogin: -1,
        /**
         * 收藏模态框
         */
        CollectModel: {
            show: false,
            newList: false,
            musicId: '',
            newModel: false,
            musicIds: [] as string[],
            allModel: false,
        },
        // 评论数量
        commentNums: 0,
        // 顶部活动的选项
        activeTop: {
            active: true,
            item: 0
        }
    }),
    actions: {
        getBaseUrl() {
            return this.baseUrl;
        },
        setUserInfo(user: User): void {
            for (const key in user) {
                if (isValidKey(key, user)) {
                    this.UserInfo[key] = user[key];
                }
            }
        },
        setUserInfoName(name: string) {
            this.UserInfo.name = name;
        },
        setUserInfoIntro(intro: string) {
            this.UserInfo.introduce = intro;
        },
        updateNameAndIntro(intro: string, name: string) {
            this.UserInfo.name = name
            this.UserInfo.introduce = intro
        },
        setUserAccount(account: string): void {
            this.UserInfo.account = account;
        },

        setSocialInfo(socialInfo: SocialInfo): void {
            for (const key in socialInfo) {
                if (isValidKey(key, socialInfo)) {
                    this.socialInfo[key] = socialInfo[key];
                }
            }
        },
        getSocialInfo() {
            return this.socialInfo;
        },
        setMyMusicList(myMusicList: MusicList[]): void {
            this.myMusicList = myMusicList;
            console.log("更新list", this.myMusicList);
            console.log("更新list", myMusicList);
        },
        getMyMusicList(): MusicList[] {
            return this.myMusicList;
        },
        delMyMusicList() {
            this.myMusicList = [];
        },
        Login() {
            this.isLogin = 1;
        },
        Logout() {
            this.isLogin = -1;
            this.UserInfo.avatar = ''
        },
        LoginNot() {
            this.isLogin = 0;
        },
        closeCollectModel() {
            this.CollectModel.show = false
            this.CollectModel.newList = false
            this.CollectModel.newModel = false
            this.CollectModel.allModel = false
            this.CollectModel.musicIds = []
        },
        openCollectModel() {
            this.CollectModel.show = true
        },
        openCollectAllModel(ids: string[]) {
            if (ids.length === 0) {
                return
            }
            this.CollectModel.show = true
            this.CollectModel.allModel = true
            this.CollectModel.musicIds = ids
        },
        openNewCollectModel() {
            this.CollectModel.show = true
            this.CollectModel.newList = true
            this.CollectModel.newModel = true
        },
        switchShowCollectModel() {
            this.CollectModel.show = !this.CollectModel.show
        },
        setCollectMusicId(id: string) {
            this.CollectModel.musicId = id;
        },
        getUserAvatar() {
            return this.UserInfo.avatar;
        },
        getUnRefUserInfo() {
            return this.UserInfo
        },
        updateUserAvatar(avatar: string) {
            this.UserInfo.avatar = avatar;
        },
        setCommentNum(num: number) {
            this.commentNums = num
        },
        switchCollectNewList() {
            if (this.CollectModel.newModel) {
                this.closeCollectModel()
                return
            }
            this.CollectModel.newList = !this.CollectModel.newList;
        },
        setActiveItem(index: number) {
            this.activeTop.item = index
        }
    },
    getters: {
        // 获取登录状态
        loginState: (state) => state.isLogin,
        getCollectModel: (state) => state.CollectModel,
        getMyMusicList: (state) => state.myMusicList,
        getUserInfo: (state) => state.UserInfo,
        getUserAvatar: (state) => state.UserInfo.avatar,
        getCommentNums: (state) => state.commentNums,
        getUserId: (state) => state.UserInfo.id,
    }
})